package com.kehutong.employee.app;

import com.kehutong.common.util.Token;
import com.kehutong.employee.entity.Employee;
import com.kehutong.employee.service.EmployeeService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.entity.FindArray;
import org.coraframework.util.Objects;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@MvcAction
@ReqMapping("/kht-bin/basic/employee")
public class EmployeeAction {
    private static final Logger logger = LoggerFactory.getLogger(EmployeeAction.class);

	@Inject
	private JdbcSession jdbcSession;
	@Inject
    private EmployeeService employeeService;

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) throws Exception {
        FindArray<Employee> find = jdbcSession.findArray(Employee.class)
                .in(jsonObject,"id")
                .like(jsonObject, "name")
                .eq("deleted", false);

        String department = jsonObject.getString("department");
        if(Objects.nonEmpty(department)){
            find.findInSet("department", department);
        }

        List<Employee> list = find.exe();
        return list;
    }

    @ReqMapping("/get")
    public Object get(Employee employee) {return employee;}

    @ReqMapping("/getEmployee")
    public Object getEmployee(Token token) throws Exception {
        logger.info("EmployeeAction.getEmployee:token------{}",token);
        final Employee employee = Pool.get(Employee.class, token.getUuid());
        logger.info("EmployeeAction.getEmployee result:------{}",employee);
        return employee;
    }
}
